﻿using System;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.ServiceModel.Description;

using Rhino.Mocks;

namespace RenewalProjects.Libraries.Wcf.EasyProxy.Tests.TestBuilders
{
	public class ChannelFactoryBuilder<TContract>
	{
		private readonly MockRepository _mock;
		private readonly IChannelFactory<TContract> _channelFactory;
		private readonly ServiceEndpoint _serviceEndpoint;

		public ChannelFactoryBuilder(MockRepository mock)
		{
			_mock = mock;
			_channelFactory = _mock.StrictMock<IChannelFactory<TContract>>();
			_serviceEndpoint = new ServiceEndpoint(new ContractDescription(
					typeof(TContract).FullName), new NetNamedPipeBinding(),
				new EndpointAddress("net.pipe://localhost/test"));
		}

		public ChannelFactoryBuilder<TContract> ExpectCreateChannel(TContract channelInstance)
		{
			_channelFactory.Expect(x => x.CreateChannel(_serviceEndpoint.Address, null)).Return(channelInstance);
			return this;
		}

		public ServiceEndpoint GetServiceEndpoint()
		{
			return _serviceEndpoint;
		}

		public IChannelFactory<TContract> GetFactory()
		{
			return _channelFactory;
		}
	}
}
